package com.example.sharesinfo.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import com.example.sharesinfo.R;
import com.example.sharesinfo.base.BaseUIActivity;
import com.example.sharesinfo.bean.UserBean;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.example.sharesinfo.utils.SpUtils;
import com.google.gson.Gson;

import androidx.annotation.Nullable;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends BaseUIActivity {
    private EditText et_username, et_password, et_phone, et_confirm_password;
    private RadioGroup rg_gender;
    private Button btn_register;
    private int gender = 1;
    private String ToastMessage = "登录成功";
    private ImageButton return_btn;
    //注册请求返回
    private static final int REGISTERSUCCESS = 1001;
    private static final int REGISTERFAILED = 1002;
    //Okhttp的请求参数
    private String urlRoot = "http://119.23.221.142/api/user/Regist?";
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case REGISTERSUCCESS:
                    SpUtils.getInstance().putString(Constants.SP_USER_NAME, et_phone.getText().toString());
                    SpUtils.getInstance().putString(Constants.SP_USER_PASSWOED, et_password.getText().toString());
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case REGISTERFAILED:
                    Toast.makeText(RegisterActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        getUserInfo();
    }


    private void doRegister() {
        //这里是拼接的请求头
        Map<String, String> headersdatas = new HashMap<String, String>();
        //这里是拼接的请求参数
        Map<String, String> datas = new HashMap<String, String>();
        //这里是post请求参数
        Map<String, String> BodyParams = new HashMap<String, String>();
        datas.put("Phonenumber", et_phone.getText().toString());
        datas.put("Password", et_password.getText().toString());
        datas.put("Username", et_username.getText().toString());
        datas.put("Gender", String.valueOf(gender));
        OkHttpUtils.getInstance().Post(urlRoot, null, datas,
                null, null, new OkHttpUtils.okHttpListener() {
                    Message message = mHandler.obtainMessage();

                    @Override
                    public void OnSuccess(String s, int code) throws JSONException {
                        JSONObject RegisterInfo = new JSONObject(s);
                        if (RegisterInfo.getString("code").equals("200")) {
                            //注册成功
                            message.what = REGISTERSUCCESS;
                            mHandler.sendMessage(message);
                        } else {
                            message.what = REGISTERFAILED;
                            message.obj = RegisterInfo.getJSONArray("data").
                                    getJSONObject(0).getString("Message");
                            mHandler.sendMessage(message);
                        }
                    }

                    @Override
                    public void OnFailed(String s) {

                    }
                });
    }

    private void getUserInfo() {
        rg_gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_male:
                        gender = 1;
                        break;
                    case R.id.rb_female:
                        gender = 0;
                        break;
                }
            }
        });
    }

    private void initView() {
        return_btn = findViewById(R.id.return_btn);
        et_username = findViewById(R.id.et_register_account);
        et_password = findViewById(R.id.et_register_password);
        et_confirm_password = findViewById(R.id.et_confirm_password);
        et_phone = findViewById(R.id.et_register_phone);
        btn_register = findViewById(R.id.btn_register);
        rg_gender = findViewById(R.id.rg_gender);
        return_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //输入框不能为空如果为空，
                // 将游标聚焦到该处
                if (TextUtils.isEmpty(et_username.getText())) {
                    et_username.requestFocus();
                    ToastMessage = "用户名不能为空";
                } else if (TextUtils.isEmpty(et_password.getText())) {
                    ToastMessage = "密码不能为空";
                } else if (TextUtils.isEmpty(et_confirm_password.getText())) {
                    ToastMessage = "再次输入密码不能为空";
                } else if (TextUtils.isEmpty(et_phone.getText())) {
                    ToastMessage = "手机号不能为空";
                } else if (et_confirm_password.getText().toString().equals(et_password.getText().toString())) {
                    doRegister();
                } else {
                    ToastMessage = "二次输入的密码不一致！";
                }
            }

        });
    }
}
